#include "OLBannerSurface.h"
#include "OLBanner.h"

#include <QParallelAnimationGroup>
#include <QPropertyAnimation>
#include <QtCore/QTimer>

OLBannerSurface::OLBannerSurface(bool autodismiss, QWidget *parent) :
    OLModalBackground(parent), m_animGroup(0)
{
    m_Banner = new OLBanner(this);

    m_animGroup = new QParallelAnimationGroup(this);
    QPropertyAnimation* animPos = new QPropertyAnimation(m_Banner, "pos");
    animPos->setDuration(500);
    animPos->setStartValue(QPoint(0,50));
    animPos->setEndValue(QPoint(0,200));
    m_animGroup->addAnimation(animPos);

    QPropertyAnimation* animOpacity = new QPropertyAnimation(m_Banner, "opacity");
    animOpacity->setDuration(600);
    animOpacity->setStartValue(0.0);
    animOpacity->setEndValue(1.0);
    m_animGroup->addAnimation(animOpacity);

    connect(this, SIGNAL(clicked()), this, SLOT(dismissBanner()));

    if( autodismiss ) {
        QTimer::singleShot(3000, this, SIGNAL(clicked()));
    }

    m_animGroup->start();
}

void OLBannerSurface::dismissBanner()
{
    if(m_animGroup->state()==QAbstractAnimation::Running) {
        // an animation is already running, pause it
        m_animGroup->pause();
    }

    m_animGroup->setDirection(QAbstractAnimation::Backward);
    m_animGroup->start();

    disconnect(SIGNAL(clicked())); // make sure the clicked signal is no longer connected
    connect(m_animGroup, SIGNAL(finished()), SLOT(deleteLater()));
}

void OLBannerSurface::handleParentResize()
{
    QSize appSize = parentWidget()->size();
    resize(appSize);
    m_Banner->adjustSize(appSize);
}

void OLBannerSurface::setText(const QString& message)
{
    m_Banner->setText(message);
}
